package com.test.iran;

import sim.engine.SimState;
import sim.engine.Steppable;
import sim.util.Bag;
import sim.util.geo.MasonGeometry;
import sim.util.geo.PointMoveTo;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.linearref.LengthIndexedLine;

public class Agent implements Steppable{
    protected Point location;
    PointMoveTo pointMoveTo = new PointMoveTo();
    
    private LengthIndexedLine segment = null;
    
    public Agent(){
    	GeometryFactory fact = new GeometryFactory();
        location = fact.createPoint(new Coordinate(10,10));
    }
    
	public Geometry getGeometry() {
		return location;
	}
	
    public void addToMap(IranTest state){	
    	int roadPath = state.random.nextInt(state.roads.getGeometries().numObjs);
        MasonGeometry mg = (MasonGeometry)state.roads.getGeometries().objs[roadPath];
        
        try{
        	segment = new LengthIndexedLine((LineString) mg.getGeometry());
        }
        catch(Exception e){
        	segment = new LengthIndexedLine((MultiLineString) mg.getGeometry());
        }
        
        pointMoveTo.setCoordinate(segment.extractPoint(segment.getStartIndex()));
        location.apply(pointMoveTo);
    } 

	public void step(SimState state) {
		IranTest iran = (IranTest)state;
		
        Bag bag = iran.agents.getObjectsWithinDistance(this.location, 1);
        
        System.out.println(location);
        System.out.println(bag.size());
        
        
        /*for(Object o : bag){
        	Agent agent = (Agent) ((MasonGeometry) o).userData;   	
        	System.out.println(agent);
        }*/
	}
    
    
}
